package net.huashitong.ssydt.provider.money.service;


import net.huashitong.ssydt.PaginationResult;
import net.huashitong.ssydt.exceptions.ParamErrorServiceException;
import net.huashitong.ssydt.exceptions.ServiceException;
import net.huashitong.ssydt.money.constant.MoneyConsts;
import net.huashitong.ssydt.money.entity.SsMoneyVipBag;
import net.huashitong.ssydt.money.entity.vo.SsMoneyVipBagInVO;
import net.huashitong.ssydt.money.entity.vo.SsMoneyVipBagQueryInVO;
import net.huashitong.ssydt.provider.money.mapper.SsMoneyVipBagMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author huiyang.yu
 */
@Service
@Transactional
public class SsMoneyVipBagServiceImpl implements SsMoneyVipBagService {

    private SsMoneyVipBagMapper mapper;

    @Autowired
    public SsMoneyVipBagServiceImpl(SsMoneyVipBagMapper mapper) {
        this.mapper = mapper;
    }

    @Override
    public List<SsMoneyVipBag> queryByState() {
        return this.mapper.queryByState(SsMoneyVipBag.State.SHOW);
    }
    @Override
    public List<SsMoneyVipBag> queryByAppType(String appType) {
        SsMoneyVipBagInVO ssMoneyVipBagInVO = new SsMoneyVipBagInVO();
        ssMoneyVipBagInVO.setAppType(appType);
        ssMoneyVipBagInVO.setState(SsMoneyVipBag.State.SHOW);

        return this.mapper.queryByAppType(ssMoneyVipBagInVO);
    }

    @Override
    public SsMoneyVipBag getById(Long goodsId) {
        return mapper.selectByPrimaryKey(goodsId);
    }

    @Override
    public PaginationResult<SsMoneyVipBag> paginationQuery(SsMoneyVipBagQueryInVO ssMoneyVipBagQueryInVO) {
        PaginationResult result = new PaginationResult();
        result.setPageNumber(ssMoneyVipBagQueryInVO.getPageNumber());
        result.setPageSize(ssMoneyVipBagQueryInVO.getPageSize());

        // 查询记录总数
        int total = mapper.queryCount(ssMoneyVipBagQueryInVO);
        result.setPageRecords(total);

        if (total == 0) {
            result.setList(new ArrayList());
        } else {
            // 查询当前页记录
            List<SsMoneyVipBag> list = mapper.query(ssMoneyVipBagQueryInVO);
            result.setList(list);
        }
        return result;
    }

    @Override
    public void insert(SsMoneyVipBag ssMoneyVipBag) {
        checkParameters(ssMoneyVipBag);

        if (ssMoneyVipBag.getNumberLimit() == null)
            ssMoneyVipBag.setNumberLimit(0);
        if (ssMoneyVipBag.getSequence() == null)
            ssMoneyVipBag.setSequence(99);

        mapper.insert(ssMoneyVipBag);
    }

    @Override
    public void updateById(SsMoneyVipBag ssMoneyVipBag) {
        // 判断记录是否存在
        SsMoneyVipBag bag = mapper.selectByPrimaryKey(ssMoneyVipBag.getId());
        if (bag == null) {
            throw new ServiceException("vip套餐不存在");
        }
        if (ssMoneyVipBag.getBagType() == null) {
            ssMoneyVipBag.setBagType(bag.getBagType());
            ssMoneyVipBag.setBagDay(bag.getBagDay());
            ssMoneyVipBag.setGmtExpiryDate(bag.getGmtExpiryDate());
        }

        checkParameters(ssMoneyVipBag);

        mapper.updateByPrimaryKeySelective(ssMoneyVipBag);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    /**
     * 入参检查
     *
     * @param ssMoneyVipBag vip套餐对象
     */
    void checkParameters(SsMoneyVipBag ssMoneyVipBag) {
        switch (ssMoneyVipBag.getNowExecute()) {
            case MoneyConsts.NowExecute.ORIGINA_PRICE:
            case MoneyConsts.NowExecute.FAVOURABLE_PRICE:
            case MoneyConsts.NowExecute.PREFERENCE_PRICE:
                break;
            default:
                throw new ParamErrorServiceException("当前执行价输入错误");
        }
        if (ssMoneyVipBag.getBagType() == null)
            ssMoneyVipBag.setBagType("");
        switch (ssMoneyVipBag.getBagType()) {
            case MoneyConsts.BagType.EFFECTIVE_DAYS:
                if (ssMoneyVipBag.getBagDay() == null)
                    throw new ParamErrorServiceException("请填写有效天数");
                if (ssMoneyVipBag.getBagDay() < 1)
                    throw new ParamErrorServiceException("有效天数不能为0");
                break;
            case MoneyConsts.BagType.DEADLINE:
                if (ObjectUtils.isEmpty(ssMoneyVipBag.getGmtExpiryDate()))
                    throw new ParamErrorServiceException("请填写截止时间");
                break;
            default:
                throw new ParamErrorServiceException("套餐类型输入错误");
        }
        switch (ssMoneyVipBag.getState()) {
            case SsMoneyVipBag.State.SHOW:
            case SsMoneyVipBag.State.HIDDEN:
                break;
            default:
                throw new ParamErrorServiceException("状态输入错误");
        }

    }
}
